package test;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/*public class WinCMD {
	public static void main(String[] args) {
		System.out.println("begin");
		String videoToImg = "ffmpeg -i E:/1.flv -r 10 -s 320*240 -f image2 D:/img/foo-%03d.jpeg";
		//String string = "cmd /c start \"D:/ff.bat\""; 
		new WinCMD().cmd(videoToImg);
		System.out.println("end");
	}
	
	
	public void cmd(String str){
		Runtime runtime = Runtime.getRuntime();
		Process process = null;
		try {
			process = runtime.exec(str);
		}catch (IOException e) {
			e.printStackTrace();
		}
		
	}
		
}*/


public class WinCMD {
	public static void main(String[] args) throws Exception {
		System.out.println("begin-------------------");
		//String str = "ffmpeg -i E:/1.flv -r 10 -s 320*240 -f image2 D:/img/foo-%03d.jpeg";
		String str = "notepad";
		new WinCMD().cmd(str);
		while(new WinCMD().getProcess("ffmpeg.exe")){
			Thread.sleep(50);
		}
		System.out.println("end-----------------");
	}
	
	
	public void cmd(String str){
		Process process=null;
		try {
			process = Runtime.getRuntime().exec(str);
			try {
				process.waitFor();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//return process.waitFor();
	}
	
	boolean getProcess(String process){
		boolean flag=false;
		try{
			Process p = Runtime.getRuntime().exec( "cmd /c tasklist ");
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			InputStream os = p.getInputStream();
			byte b[] = new byte[256];
			while(os.read(b)> 0)
			baos.write(b);
			String s = baos.toString();
			if(s.indexOf(process)>=0){
				flag=true;
			}
			else{
			flag=false;
			}
		}catch(java.io.IOException ioe){
			ioe.printStackTrace();
		}
		return flag;
	}
}
